﻿using System.Linq;
using System.Xml.Serialization;

namespace QLab.Controllers.SolutionExplorer
{

	[XmlRootAttribute(ElementName = "Solution", Namespace = "", IsNullable = false)]
	public class SolutionExplorerItemProject : SolutionExplorerItemBase
	{
		public SolutionExplorerItemProject()
			: base(SolutionExplorerItemType.Project)
		{

#if DEBUG

			#region KeyOPS Test
			//// KeyOPS Test
			//ChildItems.Add(new SolutionExplorerItemFolder() { Path = @"C:\Temp\QLib\TestProject_01\F1" });
			//ChildItems.Add(new SolutionExplorerItemFolder() { Path = @"C:\Temp\QLib\TestProject_01\F2" });
			//ChildItems.Add(new SolutionExplorerItemFolder() { Path = @"C:\Temp\QLib\TestProject_01\F3" });
			//ChildItems.Add(new SolutionExplorerItemFile() { Path = @"C:\Temp\QLib\TestProject_01\file1x.q" });
			//ChildItems.Add(new SolutionExplorerItemFile() { Path = @"C:\Temp\QLib\TestProject_01\file2x.q" });
			#endregion

#endif

		}

		public SolutionExplorerItemProject(string SolutionPath, string ProjectName)
			: base(SolutionExplorerItemType.Project)
		{
			string project_path = System.IO.Path.Combine(SolutionPath, ProjectName);

			if (!System.IO.Directory.Exists(project_path))
				System.IO.Directory.CreateDirectory(project_path);

			System.IO.DirectoryInfo dirinfo = new System.IO.DirectoryInfo(project_path);
			ProjectRootPath = dirinfo.Name;

			Name = System.IO.Path.GetFileNameWithoutExtension(ProjectName) + DefaultFileExtension;
		}

		public override void Load(string Path)
		{
			throw new System.NotImplementedException();
		}

		public override void Save()
		{
			System.IO.StreamWriter projectStreamWriter = System.IO.File.CreateText(FullPath);
			XmlSerializer projectSerializer = new XmlSerializer(typeof(SolutionExplorerItemProject));
			projectSerializer.Serialize(projectStreamWriter, this);
			projectStreamWriter.Close();
		}

		public override void Remove()
		{
			if (ChildItems != null && ChildItems.Count > 0)
				foreach (SolutionExplorerItemBase item in ChildItems.ToArray())
					item.Remove();

			if (Parent != null)
				Parent.ChildItems.Remove(this);
		}

		[XmlAttribute("Folder")]
		public string ProjectRootPath { get; set; }

	}
}
